package com.example.ovulationperiodtracker.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.ovulationperiodtracker.R;
import com.example.ovulationperiodtracker.databinding.AdapterMedicineBinding;
import com.example.ovulationperiodtracker.model.MedicineT;
import com.example.ovulationperiodtracker.utils.MyLog;
import com.necer.base.NBaseAdapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MedicineAdapter extends NBaseAdapter<MedicineT, AdapterMedicineBinding> {


    private List<MedicineT> medicineTList;


    private List<String> checkedMedicineList;
    private RecyclerView recyclerView;

    public MedicineAdapter(Context context, RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
        checkedMedicineList = new ArrayList<>();
        medicineTList = new ArrayList<>();
        medicineTList.add(new MedicineT(context.getResources().getString(R.string.pill_0), R.mipmap.ic_pill_img_0));
        medicineTList.add(new MedicineT(context.getResources().getString(R.string.pill_1), R.mipmap.ic_pill_img_1));
        medicineTList.add(new MedicineT(context.getResources().getString(R.string.pill_2), R.mipmap.ic_pill_img_2));
        medicineTList.add(new MedicineT(context.getResources().getString(R.string.pill_3), R.mipmap.ic_pill_img_3));
        medicineTList.add(new MedicineT(context.getResources().getString(R.string.pill_4), R.mipmap.ic_pill_img_4));
        medicineTList.add(new MedicineT(context.getResources().getString(R.string.pill_5), R.mipmap.ic_pill_img_5));
        medicineTList.add(new MedicineT(context.getResources().getString(R.string.pill_6), R.mipmap.ic_pill_img_6));
        medicineTList.add(new MedicineT(context.getResources().getString(R.string.pill_7), R.mipmap.ic_pill_img_7));
        medicineTList.add(new MedicineT(context.getResources().getString(R.string.pill_8), R.mipmap.ic_pill_img_8));
        medicineTList.add(new MedicineT(context.getResources().getString(R.string.pill_9), R.mipmap.ic_pill_img_9));
        medicineTList.add(new MedicineT(context.getResources().getString(R.string.pill_10), R.mipmap.ic_pill_img_10));
        medicineTList.add(new MedicineT(context.getResources().getString(R.string.pill_11), R.mipmap.ic_pill_img_11));
        medicineTList.add(new MedicineT(context.getResources().getString(R.string.pill_12), R.mipmap.ic_pill_img_12));
        medicineTList.add(new MedicineT(context.getResources().getString(R.string.pill_13), R.mipmap.ic_pill_img_13));
        medicineTList.add(new MedicineT(context.getResources().getString(R.string.pill_14), R.mipmap.ic_pill_img_14));
        medicineTList.add(new MedicineT(context.getResources().getString(R.string.pill_15), R.mipmap.ic_pill_img_15));
        medicineTList.add(new MedicineT(context.getResources().getString(R.string.pill_16), R.mipmap.ic_pill_img_16));
        medicineTList.add(new MedicineT(context.getResources().getString(R.string.pill_17), R.mipmap.ic_pill_img_17));
        medicineTList.add(new MedicineT(context.getResources().getString(R.string.pill_18), R.mipmap.ic_pill_img_18));
        medicineTList.add(new MedicineT(context.getResources().getString(R.string.pill_19), R.mipmap.ic_pill_img_19));
        medicineTList.add(new MedicineT(context.getResources().getString(R.string.pill_20), R.mipmap.ic_pill_img_20));

        setData(medicineTList);

    }


    @Override
    public AdapterMedicineBinding createBinding(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        return AdapterMedicineBinding.inflate(inflater, parent, false);
    }

    @Override
    public void bind(@NonNull AdapterMedicineBinding binding, @NonNull MedicineT item, int position) {
        binding.checkBoxHiddenPillFrag2.setOnCheckedChangeListener(null);
        binding.checkBoxHiddenPillFrag2.setChecked(false);
        binding.checkBoxHiddenPillFrag2.setChecked(checkedMedicineList.contains(item.name));
        binding.imgPill.setImageResource(item.image);
        binding.txtPillname.setText(item.name);


        binding.checkBoxHiddenPillFrag2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (checkedMedicineList.contains(item.name)) {
                    checkedMedicineList.remove(item.name);
                } else {
                    checkedMedicineList.add(item.name);
                }

                MyLog.d(" checkedMedicineList);" + String.join(",", checkedMedicineList));

                recyclerView.post(new Runnable() {
                    @Override
                    public void run() {
                        // 在这里执行数据更新
                        notifyDataSetChanged();
                    }
                });

            }
        });
    }


    public String getCheckedMedicines() {
        return String.join(",", checkedMedicineList);
    }

    public void setCheckedMedicines(String checkedMedicines) {
        if (TextUtils.isEmpty(checkedMedicines)) {
            return;
        }
        this.checkedMedicineList = new ArrayList<>(Arrays.asList(checkedMedicines.split(",")));
    }
}
